package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerService implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void saveOrUpdate(Customer customer) {
        if(customer.getId() != null){

            customerMapper.updateByPrimaryKey(customer);
        } else {
            //新建的对象  没有录入人,以及录入日期
            customer.setInputTime(new Date());
            customer.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
            customer.setSeller((Employee) SecurityUtils.getSubject().getPrincipal());
            customerMapper.insert(customer);
        }
    }


    @Override
    public Customer getCustomerById(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> getAll() {
        return customerMapper.selectAll();
    }

    @Override
    public PageInfo<Customer> selectByQuery(QueryObject queryObject) {
//        int totalCount = customerMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Customer> customers = customerMapper.selectByQuery(queryObject);
        return new PageInfo<>(customers);
    }

    @Override
    public void updateStatus(Long customerId, Integer status) {
        customerMapper.updateStatus(customerId,status);
    }

    @Override
    public List<Customer> selectCustomersByPotentialStatus(int statusPotential) {
        return customerMapper.selectCustomersByPotentialStatus(statusPotential);
    }

}
